package indi.jp.project.business.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import indi.jp.project.business.device.dao.DeviceDiscardDAO;
import indi.jp.project.business.device.entity.DeviceMaintain;
import indi.jp.project.business.device.entity.dto.DeviceChangeDTO;
import indi.jp.project.business.device.entity.dto.DeviceHistoryDTO;
import indi.jp.project.center.service.TaskService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import indi.jp.project.process.entity.FlowTaskNotifyDTO;
import indi.jp.project.process.service.FlowTaskService;
import indi.jp.project.business.device.entity.DeviceDiscard;
import indi.jp.project.business.device.service.DeviceDiscardService;
import indi.jp.project.business.device.service.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;

@Service("deviceDiscardService")
public class DeviceDiscardServiceImpl implements DeviceDiscardService {

    @Resource
    private DeviceDiscardDAO deviceDiscardDAO;

    @Resource
    private DeviceService deviceService;

    @Resource
    private TaskService taskService;

    @Resource
    private SerialUtil serialUtil;

    @Resource
    private FlowTaskService flowTaskService;

    @Override
    public JsonResult queryGrid(DeviceDiscard entity) throws Exception {
        List<DeviceDiscard> list = deviceDiscardDAO.queryList(entity);
        int count = deviceDiscardDAO.queryCount(entity);
        Datagrid<DeviceDiscard> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public JsonResult save(DeviceDiscard entity) throws Exception {
        String id;
        if (StringUtils.isBlank(entity.getId())) {
            id = String.valueOf(serialUtil.nextId());
            entity.setId(id);
            deviceDiscardDAO.insert(entity);
        } else {
            id = entity.getId();
            deviceDiscardDAO.update(entity);
        }
        flowTaskService.saveTaskFlow(id, entity.getFlowList());
        return JsonResult.success("保存成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteById(String id) throws Exception {
        deviceDiscardDAO.deleteById(id);
        flowTaskService.deleteFlowByTaskId(id);
        return JsonResult.success("删除成功");
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        DeviceDiscard deviceDiscard = deviceDiscardDAO.findById(id);
        return JsonResult.success(deviceDiscard);
    }

    @Override
    public void notify(FlowTaskNotifyDTO flowTaskNotifyDTO) throws Exception {
        if (flowTaskNotifyDTO.getNextState() == 99) {
            DeviceDiscard deviceDiscard = deviceDiscardDAO.findById(flowTaskNotifyDTO.getTaskId());
            String devices = deviceDiscard.getDevices();
            JSONArray ja = JSONArray.parseArray(devices);
            List<DeviceHistoryDTO> historyList = new ArrayList<>();
            List<DeviceChangeDTO> changeList = new ArrayList<>();
            for (int i = 0; i < ja.size(); i++) {
                JSONObject jo = ja.getJSONObject(i);
                String deviceId = jo.getString("id");
                DeviceHistoryDTO deviceHistoryDTO = new DeviceHistoryDTO();
                deviceHistoryDTO.setId(String.valueOf(serialUtil.nextId()));
                deviceHistoryDTO.setDeviceId(deviceId);
                deviceHistoryDTO.setBillId(deviceDiscard.getId());
                deviceHistoryDTO.setEventType("discard");
                deviceHistoryDTO.setEventDesc("设备报废");
                deviceHistoryDTO.setCreateBy(deviceDiscard.getCreateBy());
                historyList.add(deviceHistoryDTO);

                DeviceChangeDTO deviceChangeDTO = new DeviceChangeDTO();
                deviceChangeDTO.setDeviceId(deviceId);
                deviceChangeDTO.setState(9);
                deviceChangeDTO.setOperator(deviceDiscard.getCreateBy());
                changeList.add(deviceChangeDTO);
            }
            deviceService.updateDevice(changeList);
            deviceService.saveDeviceHistory(historyList);
        }
    }
}
